﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SilveR
{
    public partial class AnalysisSelectorWindow : ChildWindow
    {
        public string SelectedAnalysis
        {
            get //return the selected analysis as a string
            {
                string selectedAnalysis = null;

                if(lstAnalysis.SelectedItem != null)
                {
                    ListBoxItem selectedItem = (ListBoxItem)lstAnalysis.SelectedItem;
                    selectedAnalysis = selectedItem.Content.ToString();
                }

                return selectedAnalysis;
            }
        }

        public AnalysisSelectorWindow()
        {
            InitializeComponent();

            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(0.5);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            lstAnalysis.SelectedIndex = -1; //ensure that no item is selected if cancel is clicked
        }

        private void lstAnalysis_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            OKButton.IsEnabled = (lstAnalysis.SelectedValue != null);
        }


        //double click stuff...
        private bool clickedOnce = false;

        private void timer_Tick(object sender, EventArgs e)
        {
            clickedOnce = false;
        }

        private void ElementMouseDown(object sender, MouseEventArgs e)
        {
            if (clickedOnce) //then now on second click
            {
                this.DialogResult = true;
            }
            else clickedOnce = true;
        }
    }
}